package ua.kharkov.knure.pereverziev.diploma.web.util;

import java.sql.Time;
import java.text.SimpleDateFormat;

import org.apache.log4j.Logger;

import ua.kharkov.knure.pereverziev.diploma.db.entity.Route;

public class RouteUtils {

	private static final Logger log = Logger.getLogger(RouteUtils.class);
	final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");

	public String getDurationString(final Route route) {
		Time time = getDuration(route);
		return dateFormat.format(time);
	}

	public Time getDuration(final Route route) {

		long duration = 0l;

		if (route.getFlights() != null && route.getFlights().size() > 0) {

			final int lastFlightIndex = route.getFlights().size() - 1;

			final long beginningDateTime = route.getFlights().get(0)
					.getSchedule().getDepartureTime().getTime();

			final long endingDateTime = route.getFlights().get(lastFlightIndex)
					.getSchedule().getDepartureTime().getTime()
					+ route.getFlights().get(lastFlightIndex).getSchedule()
							.getFlight_duration().getTime();

			final long datesDifference = route.getFlights()
					.get(lastFlightIndex).getFlightDate().getTime()
					- route.getFlights().get(0).getFlightDate().getTime();

			duration = endingDateTime - beginningDateTime;// + datesDifference;
			System.out.println("date: " + dateFormat.format(duration));
		}

		return new Time(duration);
	}

}
